package com.naldo.goodcard;

import java.util.HashMap;
import java.util.List;

import com.naldo.goodcard.SQL.Cartao;
import com.naldo.goodcard.SQL.DbAdapter;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.google.analytics.tracking.android.EasyTracker;

public class MainActivity extends Activity {

	DbAdapter datasource;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		Button btn_adicionar = (Button) findViewById(R.id.btAdicionarCartao);

		btn_adicionar.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent addCartao = new Intent(MainActivity.this,
						AddCardActivity.class);
				MainActivity.this.startActivity(addCartao);
			}
		});
	}

	@Override
	public void onStart() {
		super.onStart();
		EasyTracker.getInstance(this).activityStart(this); // Add this method.
	}

	@Override
	public void onStop() {
		super.onStop();
		EasyTracker.getInstance(this).activityStop(this); // Add this method.
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private class StableArrayAdapter extends ArrayAdapter<Cartao> {
		HashMap<Cartao, Integer> mIdMap = new HashMap<Cartao, Integer>();

		public StableArrayAdapter(Context context, int textViewResourceId,
				List<Cartao> objects) {
			super(context, textViewResourceId, objects);

			for (int i = 0; i < objects.size(); ++i) {
				mIdMap.put((Cartao) objects.get(i), i);
			}
		}

		@Override
		public long getItemId(int position) {
			Cartao item = (Cartao) getItem(position);
			return mIdMap.get(item);
		}

		@Override
		public boolean hasStableIds() {
			return true;
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		this.onCreate(null);

		this.datasource = new DbAdapter(this);

		List<Cartao> listCartoes = datasource.selectAll();

		final ListView listview = (ListView) findViewById(R.id.listview);

		final StableArrayAdapter adapter = new StableArrayAdapter(this,
				android.R.layout.simple_list_item_1, listCartoes);
		listview.setAdapter(adapter);
		
		listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
			
			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
			{
				final Cartao item = (Cartao) parent.getItemAtPosition(position);

				DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
				    @Override
				    public void onClick(DialogInterface dialog, int which) {
				        switch (which){
				        case DialogInterface.BUTTON_POSITIVE:
				        	datasource.ExcluirCartao(item.getId());
				        	onResume();
				            break;
				        case DialogInterface.BUTTON_NEGATIVE:
				        	onResume();
				            break;
				        }
				    }
				};
				
				AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
				builder.setMessage("Deseja excluir o cartão: " + item.getNumero() + " ?").setPositiveButton("Sim", dialogClickListener)
					.setNegativeButton("Não", dialogClickListener).show();

				return true;
			}
			
		});

		listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, final View view,
					int position, long id) {
				final Cartao item = (Cartao) parent.getItemAtPosition(position);

				Intent ccCartao = new Intent(MainActivity.this,
						ConsultActivity.class);
				ccCartao.putExtra("_id", item.getId());

				MainActivity.this.startActivity(ccCartao);
			}
		});

	}

}
